﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Blog.Repositories;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.LinqToSqlProvider;

namespace MvcEngine.Blog.LinqToSqlProvider
{
    public class BlogPostCategoryRepository : Repository, IBlogPostCategoryRepository
    {

        public void DeleteAll(Guid postId)
        {
            var categories = GetAll(postId);
            using (var contextManager = CreateContextManager(false))
            {
                contextManager.Context.PostCategories().DeleteAllOnSubmit(categories);
                contextManager.Commit();
            }
        }

        public void DeleteAll(IEnumerable<BlogPostCategory> categories)
        {
            using (var contextManager = CreateContextManager(false))
            {
                contextManager.Context.PostCategories().DeleteAllOnSubmit(categories);
                contextManager.Commit();
            }
        }

        public void InsertAll(IEnumerable<BlogPostCategory> categories)
        {
            using (var contextManager = CreateContextManager(false))
            {
                contextManager.Context.PostCategories().InsertAllOnSubmit(categories);
                contextManager.Commit();
            }
        }

        public IList<BlogPostCategory> GetAll(Guid postId)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.PostCategories().Where(p => p.PostId == postId).ToList();
            }
        }
    }
}
